package com.yuanchu.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.common.utils.DateFormatUtils;
import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.PageUtils;
import com.yuanchu.common.utils.Query;
import com.yuanchu.store.dao.YoshopRechargeOrderDao;
import com.yuanchu.store.dto.QueryRechargeParamsDto;
import com.yuanchu.store.entity.YoshopRechargeOrderEntity;
import com.yuanchu.store.service.YoshopRechargeOrderService;
import com.yuanchu.store.service.YoshopUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.yuanchu.common.utils.DateFormatUtils.convertStringToSeconds;


@Service("yoshopRechargeOrderService")
public class YoshopRechargeOrderServiceImpl extends ServiceImpl<YoshopRechargeOrderDao, YoshopRechargeOrderEntity> implements YoshopRechargeOrderService {


    @Resource
    private YoshopUserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<YoshopRechargeOrderEntity> page = this.page(
                new Query<YoshopRechargeOrderEntity>().getPage(params),
                new QueryWrapper<YoshopRechargeOrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageResult<YoshopRechargeOrderEntity> queryRechargeOrder(QueryRechargeParamsDto param) {
        LambdaQueryWrapper<YoshopRechargeOrderEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(param.getPayStatus() != null && param.getPayStatus() > 0,
                YoshopRechargeOrderEntity::getPayStatus, param.getPayStatus());
        lqw.eq(param.getRechargeType() != null && param.getRechargeType() > 0,
                YoshopRechargeOrderEntity::getRechargeType, param.getRechargeType());
        String betweenTimeStart = param.getBetweenTimeStart();
        String betweenTimeEnd = param.getBetweenTimeEnd();
        if (StringUtils.isNoneEmpty(betweenTimeStart) && StringUtils.isNoneEmpty(betweenTimeEnd)){
            long start = convertStringToSeconds(betweenTimeStart);
            long end = convertStringToSeconds(betweenTimeEnd);

            lqw.between(YoshopRechargeOrderEntity::getPayTime, start,  end);
        }
        Page<YoshopRechargeOrderEntity> page = this.page(new Page<>(param.getPage(), param.getPageSize()), lqw);
        List<YoshopRechargeOrderEntity> collect = page.getRecords();
        collect.forEach(rechargeOrder -> {
            rechargeOrder.setUser(userService.queryUserByUserId(rechargeOrder.getUserId()));
            rechargeOrder.setCreateTime(DateFormatUtils.formatDatetime(rechargeOrder.getCreateTime()));
            rechargeOrder.setPayTime(DateFormatUtils.formatDatetime(rechargeOrder.getPayTime()));
        });
        String search = param.getSearch();
        if (StringUtils.isNotEmpty(search)){
            collect =collect.stream().filter(rechargeOrder -> rechargeOrder.getUser().getNickName().contains(search)).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)){
                collect =collect.stream().filter(rechargeOrder -> rechargeOrder.getOrderNo().contains(search)).collect(Collectors.toList());
            }
        }
        return PageResult.formatPageResult(collect, param.getPage(), param.getPageSize(), collect.size());

    }

}